import { Constant } from "../constant/Constant";
import EventSystem from "../gameSwitch/startGame";

// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export default class NumberShow extends cc.Component {

    @property(cc.Sprite)
    one: cc.Sprite = null;

    @property(cc.Sprite)
    two: cc.Sprite = null;

    @property(cc.Sprite)
    three: cc.Sprite = null;

    @property(cc.Sprite)
    four: cc.Sprite = null;

    @property(cc.SpriteFrame)
    num_0: cc.SpriteFrame = null;

    @property(cc.SpriteFrame)
    num_1: cc.SpriteFrame = null;

    @property(cc.SpriteFrame)
    num_2: cc.SpriteFrame = null;

    @property(cc.SpriteFrame)
    num_3: cc.SpriteFrame = null;

    @property(cc.SpriteFrame)
    num_4: cc.SpriteFrame = null;

    @property(cc.SpriteFrame)
    num_5: cc.SpriteFrame = null;

    @property(cc.SpriteFrame)
    num_6: cc.SpriteFrame = null;

    @property(cc.SpriteFrame)
    num_7: cc.SpriteFrame = null;

    @property(cc.SpriteFrame)
    num_8: cc.SpriteFrame = null;

    @property(cc.SpriteFrame)
    num_9: cc.SpriteFrame = null;

    @property(cc.Node)
    eventSys: cc.Node = null;

    start() {
        this.showNumber(Constant.PLAYER_SCORE);
    }
    /**
     * 改变积分器显示
     * @param score 
     */
    public showNumber(score: number) {
        let t = this;
        t.changeGameSpeedByScore(score);
        score = score > 9999 ? 9999 : score;
        let num = score + "";
        t.resetAllSprite();
        let numArry = num.split("");
        for (let i = 0; i < numArry.length; i++) {
            if (t.one.spriteFrame == null) {
                t.one.spriteFrame = t.getSpriteFrameFormNum(parseInt(numArry[i]));
                continue;
            }
            if (t.two.spriteFrame == null) {
                t.two.spriteFrame = t.getSpriteFrameFormNum(parseInt(numArry[i]));
                continue;
            }
            if (t.three.spriteFrame == null) {
                t.three.spriteFrame = t.getSpriteFrameFormNum(parseInt(numArry[i]));
                continue;
            }
            if (t.four.spriteFrame == null) {
                t.four.spriteFrame = t.getSpriteFrameFormNum(parseInt(numArry[i]));
                continue;
            }
        };
    }
    /**
     * 由分数改变游戏速度
     * @param score 
     */
    private changeGameSpeedByScore(score: number) {
        let speedRate = (score / 4) + 20;
        this.eventSys.getComponent(EventSystem).changeSpeed(speedRate);
    }

    /**
     * 通过数字获得对应的数字图
     * @param num 
     */
    private getSpriteFrameFormNum(num: number): cc.SpriteFrame {
        let t = this;
        switch (num) {
            case 0: return t.num_0;
            case 1: return t.num_1;
            case 2: return t.num_2;
            case 3: return t.num_3;
            case 4: return t.num_4;
            case 5: return t.num_5;
            case 6: return t.num_6;
            case 7: return t.num_7;
            case 8: return t.num_8;
            case 9: return t.num_9;
            default: return null;
        }
    }

    private resetAllSprite() {
        this.one.spriteFrame = null;
        this.two.spriteFrame = null;
        this.three.spriteFrame = null;
        this.four.spriteFrame = null;
    }
    // update (dt) {}
}
